package com.qxzn.app.medicalhealth.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.MedicineRemindBean;
import com.qxzn.app.medicalhealth.R;
import com.qxzn.app.medicalhealth.R2;
import com.qxzn.app.medicalhealth.activity.RemindDetailActivity;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.DateUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TakeMedicalRemindAdapter extends RecyclerView.Adapter<TakeMedicalRemindAdapter.ViewHolder> {

    private Context activity;
    private List<MedicineRemindBean.DataBean.ResultBean> dataList;
    private MedicineRemindBean.DataBean.ResultBean item;

    private View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MedicineRemindBean.DataBean.ResultBean item = (MedicineRemindBean.DataBean.ResultBean) v.getTag();
            activity.startActivity(new Intent(activity, RemindDetailActivity.class).putExtra("title", "吃药提醒详情").putExtra("id", item.getId()));
        }
    };
    private View.OnClickListener checkClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MedicineRemindBean.DataBean.ResultBean item = (MedicineRemindBean.DataBean.ResultBean) v.getTag();
            updateMedicineRemindState(item);
        }
    };

    public TakeMedicalRemindAdapter(Context activity, List dataList) {
        this.activity = activity;
        this.dataList = dataList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_take_medical_remind, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        item = dataList.get(position);
        holder.view.setTag(item);
        holder.view.setOnClickListener(itemClick);
        holder.ivRight.setTag(item);
        holder.ivRight.setOnClickListener(checkClick);
        List<MedicineRemindBean.DataBean.ResultBean.ResultdetailsBean> medicineList = item.getResultdetails();
        if (medicineList != null && medicineList.size() > 0) {
            MedicineRemindBean.DataBean.ResultBean.ResultdetailsBean medicine = medicineList.get(0);
            if (medicine != null) {
                holder.tvName.setText(medicine.getDrugname());
                Glide.with(activity).load(HttpRequest.BASE_URL + medicine.getUrl()).apply(new RequestOptions().placeholder(R.mipmap.logo).error(R.mipmap.logo)).into(holder.ivIcon);
            }
        }
        if (item.getType() == 1) {
            holder.tvTime.setText(("每周" + DateUtil.getWeekStrByWeekNum(item.getTypevalue(), "")) + "：" + (item.getMedicinetime() + "").replace(",", "、"));
        } else if (item.getType() == 2) {
            holder.tvTime.setText("每隔" + item.getTypevalue() + "天：" + (item.getMedicinetime() + "").replace(",", "、"));
        } else {
            holder.tvTime.setText("每天：" + (item.getMedicinetime() + "").replace(",", "、"));
        }
        if (item.getState() == 1) {
            holder.ivRight.setImageResource(R.mipmap.icon_check);
        } else {
            holder.ivRight.setImageResource(R.mipmap.icon_uncheck);
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private View view;
        @BindView(R2.id.iv_icon)
        ImageView ivIcon;
        @BindView(R2.id.iv_right)
        ImageView ivRight;
        @BindView(R2.id.tv_name)
        TextView tvName;
        @BindView(R2.id.tv_time)
        TextView tvTime;

        ViewHolder(View view) {
            super(view);
            this.view = view;
            ButterKnife.bind(this, view);
        }
    }

    private void updateMedicineRemindState(MedicineRemindBean.DataBean.ResultBean item) {
        JSONObject jsonObject = new JSONObject();
        int state;
        if (item.getState() == 1) {
            state = 2;
        } else {
            state = 1;
        }
        jsonObject.put("id", item.getId());
        jsonObject.put("state", state);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toJSONString());
        Call<BaseBean> call = HttpRequest.getService().updateMedicineRemindState(body);
        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                BaseBean bean = response.body();
                if (bean != null) {
                    if (bean.getCode() == 200) {
                        item.setState(state);
                        notifyDataSetChanged();
                        return;
                    }
                }
                if (bean != null && !TextUtils.isEmpty(bean.getMsg())) {
                    AppUtil.showToast(activity, bean.getMsg());
                } else {
                    AppUtil.showToast(activity, "网络错误，请稍后再试！");
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                AppUtil.showToast(activity, "网络错误，请稍后再试！");
            }
        });
    }

}
